<?php

class Departamento implements IObject {

    public $tMysqli;
    private $IDDepartamento;
    private $StNome;
    private $IDUsuario;
    private $tSetores;
    private $Report;

    public function __construct($IDDepartamento, $StNome, $IDUsuario = false, &$tMysqli = false) {
        $this->IDDepartamento = $IDDepartamento;
        $this->StNome = $StNome;
        $this->IDUsuario = $IDUsuario;
        $this->tMysqli = $tMysqli;
    }

    public static function loadDepartament($IDDepartamento, &$tMysqli) {
        $StSql = TQuery::getSqlDepartamentLoad($IDDepartamento);
        $tMysqli->exec($StSql);
        return new Departamento($tMysqli->result(0, 'IDDepartamento'), $tMysqli->result(0, 'StDepartamento'), false, $tMysqli);
    }

    public function loadSectors() {
        $StSql = empty($this->IDUsuario) ? TQuery::getSqlFindSectorByDepartament($this->IDDepartamento) : TQuery::getSqlFindUserSectorByDepartament($this->IDUsuario, $this->IDDepartamento);
        $tQuery = $this->tMysqli->exec($StSql);
        while ($ArSectors = mysqli_fetch_assoc($tQuery)) {
            $Sector = new Setor($ArSectors['IDSetor'], $ArSectors['IDDepartamento'], $ArSectors['StSetor'], $this->tMysqli);
            $this->tSetores[] = $Sector;
        }
    }

    public function countSectors() {
        return count($this->tSetores);
    }

    public function loadEmployeeFromSectors() {
        if(empty($this->tSetores)) { return false; }
        foreach ($this->tSetores as &$Setor) {
            $Setor->loadEmployee();
        }
    }

    //08-05
    public function hasSectors() {
        return $this->countSectors() > 0;
    }

    public function countContractorsEmployees() {
        if (empty($this->tSetores)) {
            return 0;
        }
        $contractors = 0;
        foreach ($this->tSetores as $Setor) {
            $Setor->loadEmployee();
            $contractors += $Setor->countContractorsEmployees();
        }
        return $contractors;
    }

    public function countEmployees() {
        if (empty($this->tSetores)) {
            return 0;
        }
        $contractors = 0;
        foreach ($this->tSetores as $Setor) {
            $Setor->loadEmployee();
            $contractors += $Setor->countEmployees();
        }
        return $contractors;
    }

    public function equals(IObject $Departamento) {
        return $Departamento->getIdDepartamento() == $this->IDDepartamento;
    }

    public function getIDDepartamento() {
        return $this->IDDepartamento;
    }

    public function getStNome() {
        return $this->StNome;
    }

    public function getSetores($First = false) {
        return empty($First) ? $this->tSetores : $this->tSetores[0];
    }

    public function getReport($StProp) {
        return $this->Report[$StProp];
    }

    public function setSector($IDSetor, $StNome) {
        $this->tSetores[] = new Setor($IDSetor, $this->IDDepartamento, $StNome);
    }

    public function setReport($Report) {
        $this->Report = $Report;
    }

}

?>
